import {
  bindRouter,
  deletePermission,
  insertOnePermission,
  lookAllPermission,
  lookPermission,
  lookPermissionByRole,
  lookPermissionByRouter,
  unbindRouter,
  updateOnePermission,
} from '@/api/permission'
import store from '@/store/index'

const getDefaultState = () => {
  return {
    permission: {},
    permissionList: [],
  }
}

const state = getDefaultState()

const mutations = {
  SET_INFO: (state, permission) => {
    state.permission = permission
  },
  SET_LIST: (state, permissionList) => {
    state.permissionList = permissionList
  },
}

const actions = {
  // 查询所有
  async lookAllPermission({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 缓存
      if (state.permissionList.length > 0) {
        resolve(state.permissionList)
        return
      }
      lookAllPermission()
        .then((response) => {
          commit('SET_LIST', response.data)
          resolve(response.data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 条件查询
  async lookPermission({ state }, permission) {
    return new Promise((resolve, reject) => {
      lookPermission(permission)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 添加一条权限
  async insertOnePermission({ commit, state }, permission) {
    return new Promise((resolve, reject) => {
      insertOnePermission(permission)
        .then((response) => {
          store.dispatch('permission/resetList')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 修改权限
  async updateOnePermission({ commit, state }, permission) {
    return new Promise((resolve, reject) => {
      updateOnePermission(permission)
        .then((response) => {
          store.dispatch('permission/resetList')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 批量删除权限
  async deletePermissions({ commit, state }, ids) {
    return new Promise((resolve, reject) => {
      deletePermission(ids)
        .then((response) => {
          store.dispatch('permission/resetList')
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 根据角色ID查询对应权限列表
  async lookPermissionFromRole({ statue }, roleId) {
    return new Promise((resolve, reject) => {
      lookPermissionByRole({ id: roleId })
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 根据路由ID查询对应权限列表
  async lookPermissionByRouter({ state }, routerId) {
    return new Promise((resolve, reject) => {
      lookPermissionByRouter(routerId)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 绑定路由到权限
  async bindRouter({ state }, map) {
    return new Promise((resolve, reject) => {
      bindRouter(map)
        .then((response) => {
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 解绑路由到权限
  async unbindRouter({ state }, map) {
    return new Promise((resolve, reject) => {
      unbindRouter(map)
        .then((response) => {
          resolve(response)
          store.dispatch('user/getInfo')
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 重置列表
  async resetList({ commit, state }) {
    // 置空
    if (state.routerList.length > 0) {
      commit('SET_LIST', [])
    }
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
